package com.zjuee.web.basic.captcha.service.impl;

import com.zjuee.base.constant.Common;
import com.zjuee.base.utils.NumberUtils;
import com.zjuee.core.utils.RedisUtil;
import com.zjuee.service.basic.message.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@Service(value = "phone")
public class PhoneCaptchaServiceImpl extends  AbstractCaptchaService{

    @Resource
    private MessageService messageService;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public Boolean get(String phone) {
        //生成随机六位验证码
        String random = NumberUtils.getNonceStr();
        //发送短信
        String str = messageService.sendMessage(phone,random,"211837");
        if(StringUtils.isNotBlank(str)){
            //存放在redis中 - 5分钟过期
            String redisSaltKey = Common.Cache.PHONE_CAPTCHA  + Common.Cache.SEPARATOR + random;
            redisUtil.setKey(redisSaltKey, random, Common.Captcha.CAPTCHA_TIMEOUT, TimeUnit.MINUTES);
            return true;
        }
        return false;
    }

}
